package com.bite.forum.services.impl;

import com.bite.forum.common.AppResult;
import com.bite.forum.common.ResultCode;
import com.bite.forum.dao.MessageMapper;
import com.bite.forum.exception.ApplicationException;
import com.bite.forum.model.Message;
import com.bite.forum.model.User;
import com.bite.forum.services.IMessageService;
import com.bite.forum.services.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
    // 注入MessageMapper
    @Resource
    private MessageMapper messageMapper;

    // 注入UserService
    @Resource
    private IUserService userService;

    @Override
    public void create(Message message) {
        // 校验参数是否为空
        if(message == null || message.getPostUserId() == null || message.getReceiveUserId() == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验接收者是否存在
        User user = userService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() == 1) {
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 设置消息状态和删除状态
        message.setState((byte)0);
        message.setDeleteState((byte)0);
        // 获取当前时间
        Date date = new Date();
        // 设置创建时间和更新时间
        message.setCreateTime(date);
        message.setUpdateTime(date);

        // 插入消息
        int row = messageMapper.insertSelective(message);
        // 校验插入是否成功
        if (row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Integer count = messageMapper.selectUnreadCount(receiveUserId);

        if(count == null) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        // 判断接收用户ID是否为空或小于等于0
        if(receiveUserId == null || receiveUserId <= 0) {
            // 记录警告日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 根据接收用户ID查询消息
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        // 返回消息列表
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if(id == null || id <= 0 || state < 0 || state > 2) {
            // 记录警告日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date = new Date();
        updateMessage.setUpdateTime(date);
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Message selectById(Long id) {
        if(id == null || id <= 0) {
            // 记录警告日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if(repliedId == null || repliedId <= 0) {
            // 记录警告日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Message updateMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(updateMessage == null || updateMessage.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }

        updateStateById(repliedId, (byte) 2);
        create(message);
    }
}
